<?php

  if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

  // -- webMS Modification --
  // Check if osCommerce environment is already loaded
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      // Reconnect to osCommerce Database
      tep_db_connect() or die('Unable to connect to database server!');
    }
    // Load osCommerce Globals for Boxes
    global $category_links, $languages_id, $catID, $catID_array, $current_category_id, $order, $order_total_modules, $order_total, $total_count, $total_weight, $billto, $ot_tax;
    global $request_type, $lng, $cart, $currencies, $customer_id, $language, $navigation, $breadcrumb, $shipping;
  } else {
    // Load osCommerce Environment for the First Time
    require('components/'.$option.'/includes/application_top.php');
  }

  // MVS start if a shipping method has not been selected for all vendors, redirect the customer to the shipping method selection page
  require(DIR_WS_CLASSES . 'order.php');
  $order = new order;

  //print_r($shipping);
  //die();
     if (SELECT_VENDOR_SHIPPING == 'true') { // This test only works under MVS
    
         if (!is_array ($shipping['vendor']) || count ($shipping['vendor']) != count ($cart->vendor_shipping)) { // No shipping selected or not all selected
         if (($order->content_type == 'virtual') || ($order->content_type == 'virtual_weight')){
         }else{
              tep_redirect(tep_href_link(FILENAME_CHECKOUT_SHIPPING, 'error_message=' . ERROR_NO_SHIPPING_SELECTED_SELECTED, 'SSL'));
              }
              }
           } // MVS end
  require(DIR_WS_LANGUAGES . $language . '/' . $osMod . '.php');

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id') && $ecjc_pwa != 1) {
    $navigation->set_snapshot();
    tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
//  if(!tep_session_is_registered('cc_id')){
tep_session_register('cc_id');
//}
// #################### Begin Added CGV JONYO ######################
if (!tep_session_is_registered('cot_gv'))   //added to reset whether a gift voucher is used or not on this order
tep_session_register('cot_gv');
// #################### End Added CGV JONYO ######################

if (!tep_session_is_registered('customer_shopping_points_spending')) //tep_session_unregister('customer_shopping_points_spending');

tep_session_register('customer_shopping_points_spending');

// if there is nothing in the customers cart, redirect them to the shopping cart page
  if ($cart->count_contents() < 1) {
    tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
  }



// avoid hack attempts during the checkout procedure by checking the internal cartID
  if (isset($cart->cartID) && tep_session_is_registered('cartID')) {
    if ($cart->cartID != $cartID) {
      tep_redirect(tep_href_link(FILENAME_CHECKOUT_SHIPPING, 'err=here', 'SSL'));
    }
  }

// if we have been here before and are coming back get rid of the credit covers variable
// #################### Added CGV ######################
	if(tep_session_is_registered('credit_covers')) tep_session_unregister('credit_covers');  // CCGV Contribution
    if(tep_session_is_registered('cot_gv')) tep_session_unregister('cot_gv'); //CCGV
// #################### End Added CGV ######################
//tep_session_register('credit_covers');
tep_session_register('cot_gv');

// Stock Check
  if ( (STOCK_CHECK == 'true') && (STOCK_ALLOW_CHECKOUT != 'true') ) {
    $products = $cart->get_products();
    for ($i=0, $n=sizeof($products); $i<$n; $i++) {
      if (tep_check_stock($products[$i]['id'], $products[$i]['quantity'])) {
        tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
        break;
      }
    }
  }
  

// if no billing destination address was selected, use the customers own address as default
  if (!tep_session_is_registered('billto')) {
    tep_session_register('billto');
    $billto = $customer_default_address_id;
  } else {

    
    if ( (is_array($billto) && empty($billto)) || is_numeric($billto) ) { 
        $check_address_query = tep_db_query("select count(*) as total from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . (int)$customer_id . "' and address_book_id = '" . (int)$billto . "'"); 
        $check_address = tep_db_fetch_array($check_address_query); 


       if ($check_address['total'] != '1') { 
            $billto = $customer_default_address_id; 
           if (tep_session_is_registered('payment')) tep_session_unregister('payment'); 
         } 

    }
  }

  
  //require(DIR_WS_CLASSES . 'order.php');
  $order = new order;
// #################### Added CGV ######################
  require(DIR_WS_CLASSES . 'order_total.php');//ICW ADDED FOR CREDIT CLASS SYSTEM
  $order_total_modules = new order_total;//ICW ADDED FOR CREDIT CLASS SYSTEM
  $order_total_modules->clear_posts(); // ADDED FOR CREDIT CLASS SYSTEM by Rigadin in v5.13
// #################### End Added CGV ######################

  if (!tep_session_is_registered('comments')) tep_session_register('comments');

  if (isset($_POST['comments']) && tep_not_null($_POST['comments'])) {
    $comments = tep_db_prepare_input($_POST['comments']);
  }

 $total_weight = $cart->show_weight();
  $total_count = $cart->count_contents();
// #################### Added CGV ######################
  $total_count = $cart->count_contents_virtual(); //ICW ADDED FOR CREDIT CLASS SYSTEM
// #################### End Added CGV ######################

// load all enabled payment modules
  require(DIR_WS_CLASSES . 'payment.php');
  $payment_modules = new payment;
  
if ( $order->info['subtotal']+$order->info['shipping_cost']==0 ) {
	tep_redirect(tep_href_link(FILENAME_CHECKOUT_CONFIRMATION, 'payment=freeofcharge', 'SSL'));
}
  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CHECKOUT_PAYMENT);
  

  $breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
  $breadcrumb->add(NAVBAR_TITLE_2, tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));


require(WS_TEMPLATES . E_TEMPLATE_FOLDER . '/'.$osMod.'.html');
require(DIR_WS_INCLUDES . 'footer.php'); 
require(DIR_WS_INCLUDES . 'application_bottom.php');

  // -- webMS Modification --
  // reset the Mambo Database connection (stomped by osCommerce Connection)
  if (!DB_SHARED) {
    global $database, $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  }
 ?>
